package com.xjiye_test.payServer.dao;

import com.xiaoleilu.hutool.db.Entity;
import com.xiaoleilu.hutool.db.Page;
import com.xiaoleilu.hutool.db.PageResult;
import com.xiaoleilu.hutool.db.SqlRunner;
import com.xiaoleilu.hutool.db.ds.DSFactory;
import com.xiaoleilu.hutool.db.handler.EntityHandler;
import com.xiaoleilu.hutool.db.handler.EntityListHandler;
import com.xiaoleilu.hutool.db.handler.PageResultHandler;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.xjiye_test.payServer.pojo.SmfPayInfo;
import com.xjiye_test.payServer.servletGroup.weixinpublic.count;

import java.sql.SQLException;
import java.util.List;

public class SmfOrderDao {
    final private SqlRunner runner = SqlRunner.create(DSFactory.get("group_db3"));

    public void addSmfPayInfo(SmfPayInfo pay,String sid,String payNo) throws SQLException {
        runner.insert(Entity.create("BusinessOrder")
                .set("amount",pay.getAmount())
                .set("order_no",pay.getOrder_no())
                .set("subject",pay.getSubject())
                .set("pay_type",pay.getPay_type())
                .set("pay_channel",pay.getPay_channel())
                .set("time",pay.getTime())
                .set("memo",pay.getMemo())
                .set("notify_url",pay.getNotify_url())
                .set("auth_code",pay.getAuth_code())
                .set("sid",sid)
                .set("fee", NumberUtil.round(NumberUtil.mul(pay.getAmount(),0.003),0))
                .set("status",0)
                .set("payNo",payNo)
        );
    }

    public void addSmfRefundInfo(SmfPayInfo pay,String sid,String payNo) throws SQLException {
        runner.insert(Entity.create("BusinessOrder")
                .set("amount",pay.getAmount())
                .set("order_no",pay.getOrder_no())
                .set("subject",pay.getSubject())
                .set("pay_type",pay.getPay_type())
                .set("pay_channel",pay.getPay_channel())
                .set("time",pay.getTime())
                .set("memo",pay.getMemo())
                .set("notify_url",pay.getNotify_url())
                .set("auth_code",pay.getAuth_code())
                .set("sid",sid)
                .set("fee", 0)
                .set("status",1)
                .set("payNo",payNo)
        );
    }

    public PageResult<Entity> getDataByCondition(Page page, List<String> keys,List<String> values) throws SQLException {
/*        Entity entity=Entity.create("BusinessOrder");
        for(int i=0;i<keys.size();i++){
            entity.set(keys.get(i),values.get(i));
        }*/
        StringBuffer sql=new StringBuffer("SELECT * from businessorder ");
        StringBuffer countSql=new StringBuffer("SELECT count(*) as count from businessorder ");
        if(keys.size()>0){
            sql.append("WHERE ");
        }
        if(keys.size()>0){
            countSql.append("WHERE ");
        }
        for(int i=0;i<keys.size();i++){
            sql.append(keys.get(i)+"? and ");
        }
        for(int i=0;i<keys.size();i++){
            countSql.append(keys.get(i)+"? and ");
        }
        String runSql=StrUtil.sub(sql.toString(),0,-4);
        String runCountSql=StrUtil.sub(countSql.toString(),0,-4);
        runSql+=" limit "+page.getPageNumber()*page.getNumPerPage()+","+(page.getPageNumber()+1)*page.getNumPerPage();
        Object[] objects=new Object[values.size()];
        for(int i=0;i<values.size();i++){
            objects[i]=values.get(i);
        }
        Entity count= runner.query(runCountSql,new EntityListHandler (),objects).get(0);
        return runner.query(runSql,new PageResultHandler(new PageResult(page.getPageNumber(), page.getNumPerPage(), count.getInt("count"))),objects);
        //return runner.page(entity, page);
    }

    public List<Entity> getOrderByOrderNo(String orderNo) throws SQLException {
        return runner.find(
                null,
                Entity.create("BusinessOrder").set("order_no", orderNo),
                new EntityListHandler()
        );
    }
}
